package com.common.certificate;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.math.BigInteger;
import java.security.*;
import java.security.cert.Certificate;
import java.security.cert.CertificateException;
import java.security.cert.CertificateFactory;
import java.security.cert.Extension;
import java.security.cert.X509Certificate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import javax.security.auth.x500.X500Principal;

import org.bouncycastle.asn1.ASN1ObjectIdentifier;
import org.bouncycastle.asn1.ASN1Primitive;
import org.bouncycastle.asn1.x500.X500Name;
import org.bouncycastle.cert.X509CertificateHolder;
import org.bouncycastle.cert.X509v3CertificateBuilder;
import org.bouncycastle.cert.jcajce.JcaX509v3CertificateBuilder;
import org.bouncycastle.crypto.CryptoException;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import org.bouncycastle.operator.ContentSigner;
import org.bouncycastle.operator.jcajce.JcaContentSignerBuilder;
import org.bouncycastle.x509.X509V3CertificateGenerator;

public class TCertificate {
	// 注册安全
	static {
		Security.addProvider(new BouncyCastleProvider());
	}

	// 生成自签名证书
	public static X509Certificate generateRootCertificate(String issuer, String fileName, String password)
			throws Exception {
		return cert;
	}

	// 生成自签名证书
	public static X509Certificate generateV3(String issuer, String subject, BigInteger serial, Date notBefore,

		return theCert;
	}

	// subject 为签名内容，longTime为有效期
	public static ResultCertificate sign(String rootFile, String password, String subject, String dayOrYear, int longTime)
			throws Exception {
		return r;
	}

	static boolean verifyCertificate(X509Certificate signedCert, X509Certificate signingCert) throws CryptoException {
		try {
			signedCert.verify(signingCert.getPublicKey());
		}
		catch (Exception ex) {
			return false;
		}

		return true;
	}

	public static X509Certificate getInfo(String path, String info) throws IOException, CertificateException, KeyStoreException, NoSuchAlgorithmException {

		return t;
	}

	// 打印证书信息
	private void print(String path) throws IOException, CertificateException {

	}

}
